/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

/**
 *
 * @author plato
 */
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import models.Student;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ModifyXMLFileCheck2 {

    public static void main(String argv[]) {
        try {
            String filepath = "D:\\student.xml";
            Student studentObj = new Student("fuck", "def", "address", 12, "", "note", 1);
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(filepath);
            doc.getDocumentElement().normalize();

            Node student = doc.getElementsByTagName("student").item(0);
            Element studentElement = null;
            studentElement = (Element) student;
            String gender = studentElement.getElementsByTagName("firstname").item(0).getFirstChild().getNodeValue();
            NodeList list = student.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {

                Node node = list.item(i);
                System.out.println("" + node.toString());
                // update
                if ("firstname".equals(node.getNodeName())) {
                    node.setTextContent(studentObj.getFirstName());
                }
                if ("lastname".equals(node.getNodeName())) {
                    node.setTextContent(studentObj.getLastName());
                }
                if ("address".equals(node.getNodeName())) {
                    node.setTextContent(studentObj.getAddress());
                }
                if ("age".equals(node.getNodeName())) {
                    node.setTextContent(studentObj.getAge() + "");
                }
                if ("dob".equals(node.getNodeName())) {
                    node.setTextContent(studentObj.getDob());
                }
                if ("note".equals(node.getNodeName())) {
                    node.setTextContent(studentObj.getNote());
                }

            }

        } catch (ParserConfigurationException ex) {
            Logger.getLogger(EditToXml.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(EditToXml.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(EditToXml.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
}